

from rest_framework import serializers
from orders.models import OrderInfo,OrderGoods
from goods.models import SKU

class OrderSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderInfo
        fields = [
            'order_id',
            'create_time'
        ]





# 针对主表SKU自定义序列化器
class SKUSimpleModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = [
            'name',
            'default_image'
        ]

# 定义针对OrderGoods中间表数据的序列化器
class OrderGoodsModelSerializer(serializers.ModelSerializer):
    # 主表SKU模型类对象，单一
    sku = SKUSimpleModelSerializer()

    class Meta:
        model = OrderGoods
        fields = [
            'count',
            'price',
            'sku'
        ]

# 额外定义一个订单的详细信息序列化器
class OrderDetailModelSerializer(serializers.ModelSerializer):
    user = serializers.StringRelatedField()

    # 1、代表的数据模型类是什么？
    # 答：OrderGoods
    # 2、数据是一个还是多个？
    # 答：是多个（查询集）
    # 序列化skus这个字段，就是序列化多条OrderGoods模型类对象数据
    skus = OrderGoodsModelSerializer(many=True)

    class Meta:
        model = OrderInfo
        fields = "__all__"












